//
//  YJMyViewController.m
//  SnackCourier
//
//  Created by qianfeng01 on 15/7/17.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJMyViewController.h"
#import "SDImageCache.h"


@interface YJMyViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>

@property (nonatomic,strong)NSMutableArray * dataArray;

@property (nonatomic,strong)NSMutableArray * liftArray;
@property (nonatomic,strong)NSMutableArray * rightArray;
@end

@implementation YJMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initUI];
    
}
-(void)initData{
    self.dataArray =[[NSMutableArray alloc]init];
    self.liftArray =[[NSMutableArray alloc] initWithObjects:@"关于我们",@"设置",nil];
    self.rightArray =[[NSMutableArray alloc] initWithObjects:@"零点食客",@"桀孑尐",@"389002771@qq.com", nil];
    
    NSArray *array1 =@[@"名称",@"开发者",@"联系方式", ];
    NSArray *array2 =@[@"内存清理",@"零点食客分享"];
    [self.dataArray addObject:array1];
    [self.dataArray addObject:array2];
}

-(void)initUI{
    self.tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:(UITableViewStyleGrouped)];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    self.tableView.rowHeight =50;
    self.tableView.showsVerticalScrollIndicator =NO;
    [self.view addSubview:self.tableView];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section]count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId =@"cellId";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {
        cell =[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellId];
    }
    
    cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];
    if (indexPath.section ==0) {
        cell.detailTextLabel.text =self.rightArray[indexPath.row];
    }
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/3.0)];
            UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
            imageView.image=[UIImage imageNamed:@"lingdian"];
            
            UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(10, view.frame.size.height+10, view.frame.size.width, 20)];
            label.text =self.liftArray[section];
            label.font =[UIFont systemFontOfSize:15];
            
            [view addSubview:imageView];
            [view addSubview:label];
            return view;
        }
            break;
        case 1:
        {
            UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 30)];
            UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, view.frame.size.width, 20)];
            label.text=self.liftArray[section];
            label.font=[UIFont systemFontOfSize:15];
            [view addSubview:label];
            return view;
        }
            break;
            
        default:
            break;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return self.view.frame.size.height/2.5;
    }
    return 30;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==1) {
        if (indexPath.row ==0) {
            NSString *title =[NSString stringWithFormat:@"删除缓存文件:%.3fM",[self getCachesSize]];
            UIActionSheet *sheet =[[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles: nil];
            [sheet showInView:self.view];
        }else if(indexPath.row ==1){
            [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010"  shareText:@"健康美食" shareImage:[UIImage imageNamed:@"con@3x"] shareToSnsNames:@[UMShareToEmail,UMShareToSms,UMShareToSina,UMShareToRenren] delegate:self];
        }
    }
}
-(BOOL)isDirectShareInIconActionSheet{
    return YES;
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response{
    self.tableView.frame =self.view.bounds;
    [self.tableView reloadData];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    [[SDImageCache sharedImageCache]clearMemory];
    [[SDImageCache sharedImageCache]clearDisk];
    NSString *myCacheOath =[NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];
    [[NSFileManager defaultManager]removeItemAtPath:myCacheOath error:nil];
}

- (double)getCachesSize{
    double sdSize =[[SDImageCache sharedImageCache] getSize];
    NSString *myChchePath =[NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator =[[NSFileManager defaultManager]enumeratorAtPath:myChchePath];
    double mySize =0;
    for (NSString *fileName in enumerator) {
        NSString *filePath =[myChchePath stringByAppendingPathComponent:fileName];
        NSDictionary *dict =[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        mySize += dict.fileSize;
    }
    double totalSize = (mySize +sdSize)/1024/1024;
    return totalSize;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
